//
//  YCCoreDataManager+CDProductCategory.m
//  DemoCoreData
//
//  Created by tangkexiong on 16/3/16.
//  Copyright © 2016年 tangkexiong. All rights reserved.
//

#import "YCCoreDataManager+CDProductCategory.h"

#define kEntiyProductCategory @"CDProductCategory"

#import "CDProduct.h"
#import "CDProductCategory.h"

@implementation YCCoreDataManager (CDProductCategory)

- (void)insertCategory:(YCProductCategory *)category
                 block:(void (^)(NSError *,
                                 YCProductCategory *))block {
    dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^{
        NSManagedObjectContext *context = [self managedObjectContext];
        
        CDProductCategory *temp = [NSEntityDescription insertNewObjectForEntityForName:kEntiyProductCategory inManagedObjectContext:context];
        
        temp.mName = category.mName;
        temp.mId = @([[NSDate date] timeIntervalSince1970]);
        
        NSError *error = nil;
        [context save:&error];
        
        YCProductCategory *category = [[YCProductCategory alloc] init];
        category.mId = temp.mId;
        category.mName = temp.mName;
        
        block(error, category);
    });
}

- (void)deleteCategory:(YCProductCategory *)category
                 block:(void (^)(NSError *))block {
    dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^{
        NSManagedObjectContext *context = self.managedObjectContext;
        
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:kEntiyProductCategory];
        
        request.predicate = [NSPredicate predicateWithFormat:@"mId=%@", category.mId];
        
        NSArray *result = [context executeFetchRequest:request error:nil];
        
        for (CDProductCategory *temp in result) {
            [context deleteObject:temp];
        }
        
        NSError *error = nil;
        [context save:&error];
        
        block(error);
    });
}

- (void)updateCategory:(YCProductCategory *)category
                 block:(void (^)(NSError *,
                                 YCProductCategory *))block {
    dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^{
        NSManagedObjectContext *context = self.managedObjectContext;
        
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:kEntiyProductCategory];
        
        request.predicate = [NSPredicate predicateWithFormat:@"mId=%@", category.mId];
        
        NSArray *result = [context executeFetchRequest:request error:nil];
        
        for (CDProductCategory *temp in result) {
            temp.mName = category.mName;
        }
        
        NSError *error = nil;
        [context save:&error];
        
        YCProductCategory *temp = [[YCProductCategory alloc] init];
        temp.mId = category.mId;
        temp.mName = category.mName;
        temp.mProducts = category.mProducts;
        
        block(error, temp);
    });
}

- (void)selectCategoriesBlock:(void (^)(NSError *,
                                        NSArray<YCProductCategory *> *))block{
    dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^{
        NSManagedObjectContext *context = self.managedObjectContext;
        
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:kEntiyProductCategory inManagedObjectContext:context];
        [fetchRequest setEntity:entity];
        
        NSError *error = nil;
        NSArray *fetchedObjs = [context executeFetchRequest:fetchRequest error:&error];

        NSMutableArray *array = [[NSMutableArray alloc] init];
        [fetchedObjs enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            YCProductCategory *category = [[YCProductCategory alloc] init];
            CDProductCategory *tmp = obj;
            
            category.mId = tmp.mId;
            category.mName = tmp.mName;
            
            NSMutableArray *productArray = [[NSMutableArray alloc] init];
            [tmp.mProducts enumerateObjectsUsingBlock:^(CDProduct * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                YCProduct *product = [[YCProduct alloc] init];
                CDProduct *cdProduct = obj;
                
                product.mId = cdProduct.mId;
                product.mName = cdProduct.mName;
                product.mPrice = cdProduct.mPrice;
                product.mCategory = category;
                
                [productArray addObject:product];
            }];
            category.mProducts = productArray;
            
            [array addObject:category];
        }];
        
        block(error, array);
    });
}

@end
